<template>
  <div class="container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          :model="formData"
          :rules="rules"
          ref="loginForm"
          class="login-form"
        >
          <h1>Hello</h1>
          <h2>欢迎登录yhx的运营平台</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="formData.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              show-password
              v-model="formData.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="logon-btn"
              :loading="loadingBtn"
              type="primary"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { ElNotification } from "element-plus";
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { getTime } from "@/utils/time";
import useUserStore from "@/store/modules/user";

let useStore = useUserStore();
let $router = useRouter();
let $route = useRoute();
let loadingBtn = ref(false);
let loginForm = ref();

const formData = reactive({
  username: "admin",
  password: "atguigu123",
});

// 自定义校验规则
const validateUsername = (_rule: any, value: any, callback: any) => {
  // rule即为校验规则对象，value即为表单元素文本内容
  // callback：如果符合条件，调用callback放行即可；如果不符合条件，调用callback方法并注入错误信息
  if (/^[\u4e00-\u9fa5a-zA-Z0-9]{5,12}$/.test(value)) {
    callback();
  } else {
    callback(new Error("用户名长度应为5~12字符"));
  }
};

// 自定义校验规则
const validatePassword = (_rule: any, value: any, callback: any) => {
  if (/^[\u4e00-\u9fa5a-zA-Z0-9]{6,18}$/.test(value)) {
    callback();
  } else {
    callback(new Error("密码长度应为6~18位"));
  }
};

const rules = reactive({
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "blur",
    },
    {
      trigger: "change",
      validator: validateUsername,
    },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
    {
      validator: validatePassword,
      trigger: "change",
    },
  ],
});

const login = async () => {
  // 校验成功之后才往下进行
  await loginForm.value.validate();

  loadingBtn.value = true;
  try {
    await useStore.userLogin(formData);
    let path: any = $route.query ? $route.query.redirect : "/";
    // 路由跳转
    $router.push({
      path,
    });

    //登录成功提示
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `HI,${getTime()}好！`,
    });
    loadingBtn.value = false;
  } catch (error) {
    loadingBtn.value = false;
    //登录失败提示
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
  }
};
</script>
<style scoped lang="scss">
.container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg");
}
.login-form {
  padding: 30px;
  position: relative;
  top: 30vh;
  background: url("@/assets/images/login_form.png");
  width: 80%;

  h1 {
    margin-bottom: 10px;
    color: #fff;
    font-weight: 900;
    font-size: 30px;
  }
  h2 {
    margin-bottom: 10px;
    color: #fff;
    font-weight: 900;
    font-size: 20px;
  }
  .logon-btn {
    width: 100%;
  }
}
</style>
